﻿using CTP.CRM.Core;
using CTP.CRM.Repository.Enum;
using CTP.CRM.Repository.VSystemRepository.Services;
using Volo.Abp;

namespace CTP.CRM.Repository
{
    /// <summary>
    /// 获取请求信息
    /// </summary>
    public class CurrentRequest
    {
        private static readonly IVSystemRepository _vSystemRepository = new  CTP.CRM.Repository.VSystemRepository.Services.VSystemRepository();

        /// <summary>
        /// 业务子系统id
        /// </summary>
        public static int BusinessSubSystemId => GetBusinessSubSystemId();

        /// <summary>
        /// 业务子系统信息
        /// </summary>
        public static Task<CurrentBusinessSubSystem> CurrentBusinessSubSystemAsync()
        {
            return GetCurrentBusinessSubSystem();
        }

        /// <summary>
        /// 获取业务子系统id
        /// </summary>
        /// <returns></returns>
        private static int GetBusinessSubSystemId()
        {
            int businessSubSystemId = 0;
            string _businessSubSystemId = CTPABPCore.HttpContext.Request.Headers["BusinessSubSystemId"] + string.Empty;
            if (!string.IsNullOrEmpty(_businessSubSystemId)
                && int.TryParse(_businessSubSystemId, out businessSubSystemId)
                && businessSubSystemId > 0)
                return businessSubSystemId;

            string _subSystemId = CTPABPCore.HttpContext.Request.Headers["SubSystemId"] + string.Empty;
            if (!string.IsNullOrEmpty(_subSystemId)
                && int.TryParse(_subSystemId, out businessSubSystemId)
                && businessSubSystemId > 0)
                return businessSubSystemId;

            if (businessSubSystemId <= 0)
                throw new BusinessException(message: "业务子系统Id不存在");

            return businessSubSystemId;
        }

        /// <summary>
        /// 获取业务子系统信息
        /// </summary>
        /// <returns></returns>
        private static async Task<CurrentBusinessSubSystem> GetCurrentBusinessSubSystem()
        {
            int businessSubSystemId = GetBusinessSubSystemId();
            var systemEntity = await _vSystemRepository.GetSingleAsync(t => t.Id == businessSubSystemId);
            if (systemEntity is null || systemEntity.Id <= 0)
                throw new BusinessException(message: "业务子系统不存在");

            return new CurrentBusinessSubSystem
            {
                BusinessSubSystemId = systemEntity.Id,
                CnName = systemEntity.CnName,
                EnName = systemEntity.EnName,
                SysBusinessType = systemEntity.SysBusinessType
            };
        }

        /// <summary>
        /// 目前仅支持中文、英文两种语言
        /// false:中文（默认语言）
        /// true:英文
        /// </summary>
        public static bool IsEnglish
        {
            get
            {
                var acceptLanguage = CTPABPCore.HttpContext.Request.Headers["Accept-Language"];
                if (string.Equals("zh-CN", acceptLanguage, StringComparison.CurrentCultureIgnoreCase))
                {
                    return false;
                }
                else if (string.Equals("en-US", acceptLanguage, StringComparison.CurrentCultureIgnoreCase))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 业务子系统信息
    /// </summary>
    public class CurrentBusinessSubSystem
    {
        /// <summary>
        /// 业务子系统id
        /// </summary>
        public int BusinessSubSystemId { get; set; }

        /// <summary>
        /// 业务子系统中文名
        /// </summary>
        public string CnName { get; set; } = null!;

        /// <summary>
        /// 业务子系统英文名
        /// </summary>
        public string EnName { get; set; } = null!;

        /// <summary>
        /// 业务子系统业务类型
        /// </summary>
        public VSysBusinessTypeEnum? SysBusinessType { get; set; }
    }
}
